package EnterpriseAlgorithm.LinkedList.Code.RemoveReciprocal;

import EnterpriseAlgorithm.LinkedList.Code.ListCode.Node;

public class RemoveReciprocal_Example {
    public static void main(String[] args) {
        Node head = ListNode_single_Initialization(10);
        printListNode_single(head);
        final RemoveReciprocal removeReciprocal = new RemoveReciprocal();
        //head = removeReciprocal.removeNthFromEnd(head, 4);
        head = removeReciprocal.removeNthFromEnd_2(head, 4);
        System.out.println("倒数第4删除后");
        printListNode_single(head);
    }

    //初始化单链表
    private static Node ListNode_single_Initialization(int length) {
        //链表初始化  length表示链表长度
        //(int)(Math.random() * 101) ---> [0 ~ 100] 内随机整数
        Node head = new Node((int) (Math.random() * 101), null);
        Node cur = head;
        for (int i = 1; i < length; i++) {
            cur.next = new Node((int) (Math.random() * 101), null);
            cur = cur.next;
        }
        return head;
    }

    //输出单链表
    private static void printListNode_single(Node head) {

        if (head == null) {
            System.out.println("null");
            return;
        }

        Node cur = head;
        while (true) {
            if (cur == null) {
                System.out.println();
                break;
            }
            System.out.print(cur.val + "\t");
            cur = cur.next;

        }
    }
}
